<?php
/**
 * 质检服务
 * User: poptser@foxmail.com
 * Date: 2018/3/28
 * Time: 10:50
 */
namespace App\Services\Sorting;

use App\Services\Orders\OrderService as Order;
use App\Services\Warehouse\DirectTransfersService;
use Illuminate\Support\Facades\DB;

class QualityService{


    public $order;          //主order service
    public $directTransfersService;          //调仓服务

    public function __construct(Order $order,DirectTransfersService $directTransfersService){


        $this->order = $order;
        $this->directTransfersService = $directTransfersService;
    }
    /**
     * 获取待质检订单
     * Author:poptser@foxmail.com
     * @param $wid
     * @return mixed
     */
    public function getUnQualityList($wid){
        //条件总成
        $filter = [];
        //
        $filter[] = ['wid', '=', $wid];
        $filter[] = ['shipping_id', '=', 3];
        $filter[] = ['order_status', '<>', 2];
        $filter[] = ['shipping_status', '=', 5];    //配货完成
        $filter[] = ['freeze_status', '=', 0];      //未冻结
        $filter[] = ['quality_id', '=', 0];         //未质检

        return DB::table('order_info')
            ->whereIn('pay_status', [2, 3])
            ->where($filter)
            ->select(['order_id','order_sn','consignee','mobile'])->get();

    }
    /**
     * 获取正常商品列表
     * Author:poptser@foxmail.com
     * @param $orderId
     * @return mixed
     */
    public function getGoodsListNorm($orderId){
        $orderGoods = $this->order->getGoodsList($orderId);
        $OrderGoods = [];
        foreach ($orderGoods as $good){
            $goodsNum = $good['goods_number']-$good['refund_number'];
            if($goodsNum>0) {
                $goodsInfo = DB::table("goods")->where("goods_id",$good['goods_id'])
                    ->select(['goods_img','cat_id'])->first();

                $OrderGoods[] = [
                    "goodsSn"=>$good['goods_sn'],
                    "goodsImg"=>$goodsInfo->goods_img,
                    "goodsClass"=>DB::table("category")->where("cat_id",$goodsInfo->cat_id)->value("cat_name"),
                    "goodsName"=>$good['goods_name'],
                    "goodsNum"=>$goodsNum
                ];
            }
        }
        return $OrderGoods;
    }

    /**
     * 获取当前次品退款列表
     * Author:poptser@foxmail.com
     * @param $orderId
     * @return array
     */
    public function getGoodsListLack($orderId){
        $orderGoods = $this->order->getGoodsList($orderId);
        $OrderGoods = [];
        foreach ($orderGoods as $good){
            $goodsNum = $good['poorNum']+$good['lackNum'];
            if($goodsNum>0) {
                $goodsInfo = DB::table("goods")->where("goods_id",$good['goods_id'])
                    ->select(['goods_img','cat_id'])->first();

                $OrderGoods[] = [
                    "goodsSn"=>$good['goods_sn'],
                    "goodsImg"=>$goodsInfo->goods_img,
                    "goodsClass"=>DB::table("category")->where("cat_id",$goodsInfo->cat_id)->value("cat_name"),
                    "goodsName"=>$good['goods_name'],
                    "poorNum"=>$good['poorNum'],
                    "lackNum"=>$good['lackNum'],
                ];
            }
        }
        return $OrderGoods;
    }

    /**
     * 质检退货仓调仓至门店仓/次品仓
     * Author:poptser@foxmail.com
     * @param $orderId
     * @param $goodsList
     * @param $backType 1门店仓 2次品仓
     * @param $note 备注
     * @return array|bool|string
     */
    public function setGoodsToWarehouse($orderId, $goodsList, $backType,$note){
        foreach ($goodsList as $key=>$value) {
            $goodsInfo = DB::table("goods")->where("goods_sn", "=", $value['goodsSn'])
                ->first(['spec_unit', 'supplier_code', 'consignment']);
            if ($goodsInfo) {
                $goodsList[$key]['goods_sn'] = $value['goodsSn'];
                $goodsList[$key]['goods_number'] = $value['operationsNum'];
                $goodsList[$key]['spec_unit'] = $goodsInfo->spec_unit;
                $goodsList[$key]['k3_supplier_sn'] = $goodsInfo->supplier_code;
                $goodsList[$key]['k3_vmi'] = $goodsInfo->consignment;
                unset($goodsList[$key]['goodsSn']);
                unset($goodsList[$key]['operationsNum']);
            }
        }
        $postData=[
            'wid'=>DB::table("order_info")->where("order_id","=",$orderId)->value("wid"),
            'goods'=>$goodsList,
            'note'=>$note
        ];
        try{
            $return=$this->directTransfersService->OrderGoodsBackToWarehouse($postData,$backType);
            if($return){
                return $return;
            }else{
                return '操作失败';
            }
        }catch (\Exception $exception){
            $failed['status'] = $exception->getMessage();
            $failed['code'] = 400;
            return json_encode($failed);
            //return $exception->getMessage();
        }

    }
}